package com.ym.controller.patient.self;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.ApiSign;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.entity.CouponEntity;
import com.ym.medical.domain.entity.UserHealthRecordEntity;
import com.ym.medical.domain.param.UserHealthRecordParam;
import com.ym.medical.domain.vo.UserAndHealthCoinSettingVO;
import com.ym.medical.domain.vo.UserHealthRecordVo;
import com.ym.medical.enums.ComputeWayEnum;
import com.ym.medical.enums.HealthCoinSourceEnum;
import com.ym.medical.service.ICouponSV;
import com.ym.medical.service.IUserHealthRecordSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Api(tags = "患者端-个人中心-健康币相关")
@Controller
@ApiSign
@RequestMapping(value = "/patient/self/healthCoin", headers = Constants.Api.VERSION)
public class HealthCoinController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(HealthCoinController.class);

	@Autowired
	private IUserHealthRecordSV userHealthRecordSV;

	@Autowired
	private ICouponSV couponSV;

	@ApiOperation("用户签到")
	@GetMapping(value = "/update")
	@ResponseBody
	public APIResponse<UserHealthRecordVo> updateHealthCoin() throws Exception {
		Long userId = ShiroUtil.getLoginId();
		//检查健康币获取情况并增加健康币变动
		return successData(userHealthRecordSV.checkHealthCoin(userId, HealthCoinSourceEnum.SIGN_IN.getKey(), ComputeWayEnum.INCREASE.getKey()));
	}

	@ApiOperation("邀请好友")
	@GetMapping(value = "/inviteFriend")
	@ResponseBody
	public APIResponse<UserHealthRecordVo> inviteFriend(UserHealthRecordEntity userHealthRecord) throws Exception {
		if (userHealthRecord.getUserId() == null) {
			throw new BusinessException("邀请好友用户主键(userId)不能为空");
		}
		return successData(userHealthRecordSV.checkHealthCoin(userHealthRecord.getUserId(), HealthCoinSourceEnum.INVITE_FRIENDS.getKey(), ComputeWayEnum.INCREASE.getKey()));
	}

	@ApiOperation("兑换优惠劵")
	@GetMapping(value = "/exchangeCoupon")
	@ResponseBody
	public APIResponse exchangeCoupon(@ApiParam(name = "id", value = "优惠劵主键", required = true) @RequestParam Long id) throws Exception {
		CouponEntity coupon = couponSV.selectById(id);
		if (coupon == null) {
			throw new BusinessException("该优惠劵不可用");
		}
		Long userId = ShiroUtil.getLoginId();
		userHealthRecordSV.exchangeCoupon(coupon, userId);
		return successMsg("兑换优惠劵成功");
	}

	@ApiOperation("健康币列表查询")
	@GetMapping(value = "/healthCoinSourceSetting/list")
	@ResponseBody
	public APIResponse<UserAndHealthCoinSettingVO> listHealthCoinSourceSetting() {
		return successData(userHealthRecordSV.checkUserCondition(ShiroUtil.getLoginId()));
	}

	@ApiOperation("个人获取健康币记录")
	@GetMapping(value = "/record/list")
	@ResponseBody
	public APIResponse<PageInfo<UserHealthRecordEntity>> listHealthCoinRecord(
			UserHealthRecordParam model,
			@RequestParam(defaultValue = "1", required = false) int pageNum,
			@RequestParam(defaultValue = "5", required = false) int pageSize
	) {
		PageDomain.startPage(pageNum, pageSize);
		List<UserHealthRecordEntity> datas = userHealthRecordSV.selectList(
				TK.select(UserHealthRecordEntity.class).where()
						.andEqualTo(UserHealthRecordEntity::getUserId, ShiroUtil.getLoginId())
						.end()
						.orderByClause("create_time desc")
		);
		return successPage(datas);
	}

 /*   @ApiOperation("优惠劵列表")
    @GetMapping(value = "/listCoupon")
    @ResponseBody
    public APIResponse<PageInfo<Coupon>> listCoupon(@ApiParam(name = "id", value = "优惠劵实体", required = true) CouponModel couponModel,
                                    @RequestParam(defaultValue = "1", required = false) int pageNum,
                                     @RequestParam(defaultValue = "5", required = false) int pageSize) {
        couponModel.setOrderByClause("create_time desc");
        return successData(couponSV.listPage(couponModel,pageNum,pageSize));
    }*/
}
